package com.example.helloword.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.example.helloword.BR;
import com.example.helloword.R;
import com.example.helloword.background.BackgroudThreadpoolService;
import com.example.helloword.background.EventManager;
import com.example.helloword.background.SourceListener;
import com.example.helloword.bean.Book;
import com.example.helloword.databinding.ActivitySearchBinding;
import com.example.helloword.request.RequestBookController;
import com.example.helloword.ui.recyclerview.BindingToRecyclerviewAdapter;
import com.githang.statusbar.StatusBarCompat;

import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends AppCompatActivity {

   List<Book> books = new ArrayList<>();
   Book book;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_search);
        ActivitySearchBinding viewDataBinding = (ActivitySearchBinding)DataBindingUtil.setContentView(this, R.layout.activity_search);
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.white));

        viewDataBinding.booksRv.setLayoutManager(new LinearLayoutManager(this));
        BindingToRecyclerviewAdapter bindingToRecyclerviewAdapter = new BindingToRecyclerviewAdapter(books, R.layout.rv_item_books, BR.book);
        bindingToRecyclerviewAdapter.setItemClickListener(new BindingToRecyclerviewAdapter.ItemClickListener() {
            @Override
            public void setClickEvent(ViewDataBinding binding, int position) {
                binding.getRoot().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        book=books.get(position);
                        Log.v("setClickEvent","book:"+book.hashCode());
                        BackgroudThreadpoolService.getInstance().getTheadpool().submit(new RequestBookController(RequestBookController.CHAPTER_LIST,books.get(position).getBookurl(),null));
//                        Intent intent = new Intent(SearchActivity.this, IntrodutionToBookActivity.class);
//                        intent.putExtra("book",books.get(position));
//                        startActivity(intent);
                    }
                });
            }
        });
        viewDataBinding.booksRv.setAdapter(bindingToRecyclerviewAdapter);
        viewDataBinding.searchTv.setOnClickListener((View v)->{


            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

            if (imm != null) {

                imm.hideSoftInputFromWindow(viewDataBinding.searchEv.getWindowToken(),  0);
            }
            BackgroudThreadpoolService.getInstance().getTheadpool().submit(new RequestBookController(RequestBookController.BOOK_LIST,null,viewDataBinding.searchEv.getText().toString()));
        });

        EventManager.getInstance().addUserListListener(new SourceListener() {
            @Override
            public void getBookList(List book_list) {
                books=book_list;
                bindingToRecyclerviewAdapter.setItems(book_list);
                bindingToRecyclerviewAdapter.notifyDataSetChanged();
            }

            @Override
            public void getChapterList(List chapter_list) {
                book.setChapters(chapter_list);
                runOnUiThread(()-> {
                        Intent intent = new Intent(SearchActivity.this, IntrodutionToBookActivity.class);
                        intent.putExtra("book",book);
                       startActivity(intent);
                    });



            }

            @Override
            public void getContent(String content) {

            }
        });



    }
}